function fn() {
    var max = 10;
    return function bar(x) {
        if (x > max) {
            console.log(x)
        }
    }
}

var f1 = fn(),
    max = 100

f1(15)

console.log("==========================")
/*闭包与变量*/
function createFunctions() {
    var result = new Array();
    for (var i = 0; i < 10; i++) {
        result[i] = function() {
            return i;
        };
    }
    return result;
}
var funcs = createFunctions();
for (var i = 0; i < funcs.length; i++) {
    console.log(funcs[i]())
}

console.log("==========================")
function createFunctions2() {
    var result = new Array();
    for (var i = 0; i < 10; i++) {
        result[i] = function(x) {
            return function() {
                return x;
            };
        }(i);
    }
    return result;
}
var funcs2 = createFunctions2();
//循环输出0-10
for (var j = 0; j < funcs2.length; j++) {
    console.log(funcs2[j]())
}
console.log("==========================")
/*引用类型*/
function setName(obj) {
    obj.name = 'aaa';
    // 如果是按引用传递的,此处传参进来obj应该被重新引用新的内存单元
    var obj = new Object();
    obj.name = 'ccc';
    return obj
}
var person = new Object();
person.name = 'bbb';
console.log(person.name) // bbb
var newPerson = setName(person);
console.log(person.name + ' | ' + newPerson.name); // aaa | ccc

var a = {
    num: '1'
}
var b = {
    num: '2'
}
function change(obj){
    obj.num = '3'
    obj=b
    return obj.num
}
var result = change(a);
console.log(result +' | '+a.num) // 2 | 3
/*
* 首先把a的值传到change函数内，obj.num = ‘3’;后a.name被修改为3;
* a的地址被换成b的地址;
* 返回此时的a中a.num。
*/

console.log("==========================")

var scope="global";
function scopeTest() {
    var scope;
    console.log(scope);
    scope="local";
}
scopeTest(); //undefined
console.log("==========================")

var scope2="global";
function scopeTest2() {
    console.log(scope2);
    scope2="local";
}
scopeTest2(); //global
